<?php

class ThongKeLoiNhuanController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }


        //Trường hợp action tuần
        if ($action == 'thongketuan') {


            $idbep = $_POST['BEP_ID'];
            $thang = $_POST['THANG'];
            $tuan = $_POST['TUAN'];
            $beps = $this->getBepId($idbep);


            if(($thang==1) && ($tuan==1)){
                $tongthu=0;
                $tongchi=0;
                $tongxuatkho=0;
                $loinhuan=0;
                $tienmat=0;

                //Tổng thu
                $results = $this->showdulieu_thu($idbep, $thang, $tuan);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }
                //Tổng chi
                $results1 = $this->showdulieu_chi($idbep, $thang, $tuan);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }
                //Tổng xuất kho
                $results2 = $this->showdulieu_xuatkho($idbep, $thang, $tuan);
                if ($results2->getKK_TONG_KHO() != null) {
                $tongxuatkho=$results2->getKK_TONG_KHO();
                }

                $loinhuan=$tongthu - $tongchi - $tongxuatkho;
                $tienmat=$tongthu - $tongchi;


                //Trường hợp

                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                            
                $dulieu = ' <thead>
                    <tr  class="ui-bar-d">
                        <th data-priority="5">Bếp</th>
                        <th data-priority="3">Tuần</th>
                        <th data-priority="5">Tháng</th>
                        <th data-priority="5">Từ ngày đến ngày</th>
                        <th data-priority="1">Lợi nhuận</th>
                        <th data-priority="1">Tiền mặt</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>' . $beps->getBEP_TEN_BEP() . '</td>
                        <td>' . $tuan . '</td>
                        <td>' . $thang . '</td>
                        <td>' . $results->getMIN_XUAT_NGAY_XUAT() . ' đến ' . $results->getMAX_XUAT_NGAY_XUAT() . '</td>     
                        <td>' . Utility::formatToCurrencyString($loinhuan) . ' (VNĐ)</td>
                        <td>' . Utility::formatToCurrencyString($tienmat) . ' (VNĐ)</td>
                    </tr>
       
                </tbody>';
            } else {
                $dulieu = '<thead>
                    <tr  class="ui-bar-d">
                            <th data-priority="5">Bếp</th>
                            <th data-priority="3">Tuần</th>
                            <th data-priority="5">Tháng</th>
                            <th data-priority="5">Từ ngày đến ngày</th>
                            <th data-priority="1">Lợi nhuận</th>
                            <th data-priority="1">Tiền mặt</th>
                    </tr>
                </thead> ';
            }
            echo $dulieu;
            exit();
                //End trường hợp







            }
            else {
                $tuan_=$tuan-1;
                $thang_=$thang-1;
                if( $tuan_==0 && ($thang>1)){


                $tongthu=0;
                $tongchi=0;
                $loinhuan=0;
                $tienmat=0;
                    
                $tongtienxuatkho=0;
                $tongtienxuatkhotuannay=0;
                $tongtienxuatkhotuantruoc=0;
                 //Tổng thu
                $results = $this->showdulieu_thu($idbep, $thang, $tuan);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }
                //Tổng chi
                $results1 = $this->showdulieu_chi($idbep, $thang, $tuan);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }
                //Tổng xuất kho
                $results_ = $this->showdulieu_xuatkho($idbep, $thang_, 4);
                $tongtienxuatkhotuantruoc=$results_->getKK_TONG_KHO();

                $results2 = $this->showdulieu_xuatkho($idbep, $thang, $tuan);
                $tongtienxuatkhotuannay=$results2->getKK_TONG_KHO();

                $tongtienxuatkho=$tongtienxuatkhotuantruoc-$tongtienxuatkhotuannay;

               
                $loinhuan=$tongthu - $tongchi - $tongtienxuatkho;
                $tienmat=$tongthu - $tongchi;

                

                 //Trường hợp

                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                            
                $dulieu = ' <thead>
                    <tr  class="ui-bar-d">
                        <th data-priority="5">Bếp</th>
                        <th data-priority="3">Tuần</th>
                        <th data-priority="5">Tháng</th>
                        <th data-priority="5">Từ ngày đến ngày</th>
                        <th data-priority="1">Lợi nhuận</th>
                        <th data-priority="1">Tiền mặt</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>' . $beps->getBEP_TEN_BEP() . '</td>
                        <td>' . $tuan . '</td>
                        <td>' . $thang . '</td>
                        <td>' . $results->getMIN_XUAT_NGAY_XUAT() . ' đến ' . $results->getMAX_XUAT_NGAY_XUAT() . '</td>     
                        <td>' . Utility::formatToCurrencyString($loinhuan) . ' (VNĐ)</td>
                        <td>' . Utility::formatToCurrencyString($tienmat) . ' (VNĐ)</td>
                    </tr>
       
                </tbody>';
                } else {
                    $dulieu = '<thead>
                        <tr  class="ui-bar-d">
                                <th data-priority="5">Bếp</th>
                                <th data-priority="3">Tuần</th>
                                <th data-priority="5">Tháng</th>
                                <th data-priority="5">Từ ngày đến ngày</th>
                                <th data-priority="1">Lợi nhuận</th>
                                <th data-priority="1">Tiền mặt</th>
                        </tr>
                    </thead> ';
                }
                echo $dulieu;
                exit();
                //End trường hợp







                }else{
                $tuan_=$tuan-1;

                $tongthu=0;
                $tongchi=0;
                $loinhuan=0;
                $tienmat=0;
                $tongtienxuatkho=0;
                $tongtienxuatkhotuannay=0;
                $tongtienxuatkhotuantruoc=0;



                //Tổng thu
                $results = $this->showdulieu_thu($idbep, $thang, $tuan);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }
                //Tổng chi
                $results1 = $this->showdulieu_chi($idbep, $thang, $tuan);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }
                //Tổng xuất kho
                $results_ = $this->showdulieu_xuatkho($idbep, $thang, $tuan_);
                $tongtienxuatkhotuantruoc=$results_->getKK_TONG_KHO();

                $results2 = $this->showdulieu_xuatkho($idbep, $thang, $tuan);
                $tongtienxuatkhotuannay=$results2->getKK_TONG_KHO();

                $tongtienxuatkho=$tongtienxuatkhotuantruoc-$tongtienxuatkhotuannay;

                $loinhuan=$tongthu - $tongchi - $tongtienxuatkho;
                $tienmat=$tongthu - $tongchi;

                

                 //Trường hợp

                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                            
                $dulieu = ' <thead>
                    <tr  class="ui-bar-d">
                        <th data-priority="5">Bếp</th>
                        <th data-priority="3">Tuần</th>
                        <th data-priority="5">Tháng</th>
                        <th data-priority="5">Từ ngày đến ngày</th>
                        <th data-priority="1">Lợi nhuận</th>
                        <th data-priority="1">Tiền mặt</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>' . $beps->getBEP_TEN_BEP() . '</td>
                        <td>' . $tuan . '</td>
                        <td>' . $thang . '</td>
                        <td>' . $results->getMIN_XUAT_NGAY_XUAT() . ' đến ' . $results->getMAX_XUAT_NGAY_XUAT() . '</td>     
                        <td>' . Utility::formatToCurrencyString($loinhuan) . ' (VNĐ)</td>
                        <td>' . Utility::formatToCurrencyString($tienmat) . ' (VNĐ)</td>
                    </tr>
       
                </tbody>';
                } else {
                    $dulieu = '<thead>
                        <tr  class="ui-bar-d">
                                <th data-priority="5">Bếp</th>
                                <th data-priority="3">Tuần</th>
                                <th data-priority="5">Tháng</th>
                                <th data-priority="5">Từ ngày đến ngày</th>
                                <th data-priority="1">Lợi nhuận</th>
                                <th data-priority="1">Tiền mặt</th>
                        </tr>
                    </thead> ';
                }
                echo $dulieu;
                exit();
                //End trường hợp





                
                }

            }


           



            }



        //Kết thúc action tuần


        //Trường hợp action  tháng
        else if ($action == 'thongkethang') {

                $idbep = $_POST['BEP_ID'];
                $thang = $_POST['THANG'];
                $beps = $this->getBepId($idbep);
                //Thống kê  theo tháng

                if($thang==1){

                    $tongthu=0;
                    $tongchi=0;
                    $tongxuatkho=0;
                    $loinhuan=0;
                    $tienmat=0;

                    //Tổng thu
                    $results = $this->showdulieu_thu_thang($idbep, $thang);
                    if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                    $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                    }

                    //Tổng chi
                    $results1 = $this->showdulieu_chi_thang($idbep, $thang);
                    if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                    $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                    }

                    //Tổng xuất kho
                    $results2 = $this->showdulieu_xuatkho_thang($idbep, $thang);
                    if ($results2->getKK_TONG_KHO() != null) {
                    $tongxuatkho=$results2->getKK_TONG_KHO();
                    }

                    $loinhuan=$tongthu - $tongchi - $tongxuatkho;
                    $tienmat=$tongthu - $tongchi;

                   //Trường hợp

                    if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                                
                        $dulieu = ' <thead>
                            <tr  class="ui-bar-d">
                                <th data-priority="5">Bếp</th>
                                <th data-priority="5">Tháng</th>
                                <th data-priority="5">Từ ngày đến ngày</th>
                                <th data-priority="1">Lợi nhuận</th>
                                <th data-priority="1">Tiền mặt</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td>' . $beps->getBEP_TEN_BEP() . '</td>
                                <td>' . $thang . '</td>
                                <td>' . $results->getMIN_XUAT_NGAY_XUAT() . ' đến ' . $results->getMAX_XUAT_NGAY_XUAT() . '</td>     
                                <td>' . Utility::formatToCurrencyString($loinhuan) . ' (VNĐ)</td>
                                <td>' . Utility::formatToCurrencyString($tienmat) . ' (VNĐ)</td>
                            </tr>
               
                        </tbody>';
                    } else {
                        $dulieu = '<thead>
                            <tr  class="ui-bar-d">
                                    <th data-priority="5">Bếp</th>
                                    <th data-priority="5">Tháng</th>
                                    <th data-priority="5">Từ ngày đến ngày</th>
                                    <th data-priority="1">Lợi nhuận</th>
                                    <th data-priority="1">Tiền mặt</th>
                            </tr>
                        </thead> ';
                    }
                    echo $dulieu;
                    exit();
                        //End trường hợp




                }else{
                    $tongthu=0;
                    $tongchi=0;
                    $loinhuan=0;
                    $tienmat=0;
                    $thang_=  $thang-1; 
                    $tongtienxuatkho=0;
                    $tongtienxuatkhotuannay=0;
                    $tongtienxuatkhotuantruoc=0;

                      //Tổng thu
                    $results = $this->showdulieu_thu_thang($idbep, $thang);
                    if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                    $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                    }

                    //Tổng chi
                    $results1 = $this->showdulieu_chi_thang($idbep, $thang);
                    if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                    $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                    }

                    //Tổng xuất kho
                    $results_ = $this->showdulieu_xuatkho_thang($idbep, $thang_);
                    $tongtienxuatkhotuantruoc=$results_->getKK_TONG_KHO();

                    $results2 = $this->showdulieu_xuatkho_thang($idbep, $thang);
                    $tongtienxuatkhotuannay=$results2->getKK_TONG_KHO();

                    $tongtienxuatkho=$tongtienxuatkhotuantruoc-$tongtienxuatkhotuannay;

                   
                    $loinhuan=$tongthu - $tongchi - $tongtienxuatkho;
                    $tienmat=$tongthu - $tongchi;

                   //Trường hợp

                    if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                                
                        $dulieu = ' <thead>
                            <tr  class="ui-bar-d">
                                <th data-priority="5">Bếp</th>
                                <th data-priority="5">Tháng</th>
                                <th data-priority="5">Từ ngày đến ngày</th>
                                <th data-priority="1">Lợi nhuận</th>
                                <th data-priority="1">Tiền mặt</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td>' . $beps->getBEP_TEN_BEP() . '</td>
                                <td>' . $thang . '</td>
                                <td>' . $results->getMIN_XUAT_NGAY_XUAT() . ' đến ' . $results->getMAX_XUAT_NGAY_XUAT() . '</td>     
                                <td>' . Utility::formatToCurrencyString($loinhuan) . ' (VNĐ)</td>
                                <td>' . Utility::formatToCurrencyString($tienmat) . ' (VNĐ)</td>
                            </tr>
               
                        </tbody>';
                    } else {
                        $dulieu = '<thead>
                            <tr  class="ui-bar-d">
                                    <th data-priority="5">Bếp</th>
                                    <th data-priority="5">Tháng</th>
                                    <th data-priority="5">Từ ngày đến ngày</th>
                                    <th data-priority="1">Lợi nhuận</th>
                                    <th data-priority="1">Tiền mặt</th>
                            </tr>
                        </thead> ';
                    }
                    echo $dulieu;
                    exit();
                        //End trường hợp

 
                }

                //Kết thúc thống kê theo tháng
               

            }

        //Kết thúc trường hợp action tháng





       





        if ($task == '') {
            $viewFile = $dir . '/views/loinhuan.php';
            $xtemplate = new XTemplate($viewFile);

            $idbep = '';
            $tenbep = '';
            $thang = 0;
            $tuan = 0;


            $maxthang = $this->getMaxThang();
            for ($k = 1; $k <= $maxthang->getTHANG_ID(); $k++) {

                if ($maxthang->getTHANG_ID() == $k) {
                    $xtemplate->assign('THANG', '<option selected="selected" value="' . $k . '">Tháng ' . $k . '</option>');
                    $thang = $k;
                } 
                else {
                    $xtemplate->assign('THANG', '<option value="' . $k . '">Tháng ' . $k . '</option>');
                }                   
                $xtemplate->parse('main.thang_');
            }

            $maxthang_ = $this->getMaxThang();
            for ($k = 1; $k <= $maxthang_->getTHANG_ID(); $k++) {

                if ($maxthang->getTHANG_ID() == $k) {
                    $xtemplate->assign('THANG', '<option selected="selected" value="' . $k . '">Tháng ' . $k . '</option>');
                    $thang = $k;
                } 
                else {
                    $xtemplate->assign('THANG', '<option value="' . $k . '">Tháng ' . $k . '</option>');
                }                   
                $xtemplate->parse('main.thang');
            }
            
            
            $maxtuan_thang = $this->getMaxTuan($maxthang->getTHANG_ID());
            for ($i = 1; $i <= $maxtuan_thang->getTUAN_ID(); $i++) {
                if ($maxtuan_thang->getTUAN_ID() == $i) {
                    $xtemplate->assign('TUAN', '<option selected="selected" value="' . $i . '">Tuần ' . $i . '</option>');
                    $tuan = $i;
                } else {
                    $xtemplate->assign('TUAN', '<option value="' . $i . '">Tuần ' . $i . '</option>');
                }
                $xtemplate->parse('main.tuan');
            }

            $beps = $this->getAllBep();
            $index = 1;
            if ($beps != null) {

                foreach ($beps as $bep) {

                    if ($index == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                    $index++;
                }
            }

            $beps_ = $this->getAllBep();
            $index_ = 1;
            if ($beps_ != null) {

                foreach ($beps_ as $bep) {

                    if ($index_ == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep_');
                    $index_++;
                }
            }
            
            $beps__ = $this->getAllBep();
            $index__ = 1;
            if ($beps__ != null) {

                foreach ($beps__ as $bep) {

                    if ($index__ == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep__');
                    $index__++;
                }
            }
            
            
           //Thống kê theo tuần


            if(($thang==1) && ($tuan==1)){
                $tongthu=0;
                $tongchi=0;
                $tongxuatkho=0;
                $loinhuan=0;
                $tienmat=0;

                //Tổng thu
                $results = $this->showdulieu_thu($idbep, $thang, $tuan);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }
                //Tổng chi
                $results1 = $this->showdulieu_chi($idbep, $thang, $tuan);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }
                //Tổng xuất kho
                $results2 = $this->showdulieu_xuatkho($idbep, $thang, $tuan);
                if ($results2->getKK_TONG_KHO() != null) {
                $tongxuatkho=$results2->getKK_TONG_KHO();
                }

                $loinhuan=$tongthu - $tongchi - $tongxuatkho;
                $tienmat=$tongthu - $tongchi;

                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                $xtemplate->assign('TUAN', $tuan);
                $xtemplate->assign('TUNGAY', $results->getMIN_XUAT_NGAY_XUAT());
                $xtemplate->assign('DENNGAY', $results->getMAX_XUAT_NGAY_XUAT());
                $xtemplate->assign('LOINHUAN', Utility::formatToCurrencyString($loinhuan));
                 $xtemplate->assign('TIENMAT', Utility::formatToCurrencyString($tienmat));
                $xtemplate->parse('main.thongke');




            }
            else {
                $tuan_=$tuan-1;
                $thang_=$thang-1;
                if( $tuan_==0 && ($thang>1)){


                $tongthu=0;
                $tongchi=0;
                $loinhuan=0;
                $tienmat=0;
                    
                $tongtienxuatkho=0;
                $tongtienxuatkhotuannay=0;
                $tongtienxuatkhotuantruoc=0;
                 //Tổng thu
                $results = $this->showdulieu_thu($idbep, $thang, $tuan);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }
                //Tổng chi
                $results1 = $this->showdulieu_chi($idbep, $thang, $tuan);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }
                //Tổng xuất kho
                $results_ = $this->showdulieu_xuatkho($idbep, $thang_, 4);
                $tongtienxuatkhotuantruoc=$results_->getKK_TONG_KHO();

                $results2 = $this->showdulieu_xuatkho($idbep, $thang, $tuan);
                $tongtienxuatkhotuannay=$results2->getKK_TONG_KHO();

                $tongtienxuatkho=$tongtienxuatkhotuantruoc-$tongtienxuatkhotuannay;

               
                $loinhuan=$tongthu - $tongchi - $tongtienxuatkho;
                $tienmat=$tongthu - $tongchi;

                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                $xtemplate->assign('TUAN', $tuan);
                $xtemplate->assign('TUNGAY', $results->getMIN_XUAT_NGAY_XUAT());
                $xtemplate->assign('DENNGAY', $results->getMAX_XUAT_NGAY_XUAT());
                $xtemplate->assign('LOINHUAN', Utility::formatToCurrencyString($loinhuan));
                 $xtemplate->assign('TIENMAT', Utility::formatToCurrencyString($tienmat));
                $xtemplate->parse('main.thongke');


                }else{
                $tuan_=$tuan-1;

                $tongthu=0;
                $tongchi=0;
                $loinhuan=0;
                $tienmat=0;
                $tongtienxuatkho=0;
                $tongtienxuatkhotuannay=0;
                $tongtienxuatkhotuantruoc=0;



                //Tổng thu
                $results = $this->showdulieu_thu($idbep, $thang, $tuan);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }
                //Tổng chi
                $results1 = $this->showdulieu_chi($idbep, $thang, $tuan);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }
                //Tổng xuất kho
                $results_ = $this->showdulieu_xuatkho($idbep, $thang, $tuan_);
                $tongtienxuatkhotuantruoc=$results_->getKK_TONG_KHO();

                $results2 = $this->showdulieu_xuatkho($idbep, $thang, $tuan);
                $tongtienxuatkhotuannay=$results2->getKK_TONG_KHO();

                $tongtienxuatkho=$tongtienxuatkhotuantruoc-$tongtienxuatkhotuannay;

                $loinhuan=$tongthu - $tongchi - $tongtienxuatkho;
                $tienmat=$tongthu - $tongchi;

                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                $xtemplate->assign('TUAN', $tuan);
                $xtemplate->assign('TUNGAY', $results->getMIN_XUAT_NGAY_XUAT());
                $xtemplate->assign('DENNGAY', $results->getMAX_XUAT_NGAY_XUAT());
                $xtemplate->assign('LOINHUAN', Utility::formatToCurrencyString($loinhuan));
                 $xtemplate->assign('TIENMAT', Utility::formatToCurrencyString($tienmat));
                $xtemplate->parse('main.thongke');
                
                }

            }

            //Kết thúc thống kê theo tuần
           

            //Thống kê  theo tháng

            if($thang==1){

                $tongthu=0;
                $tongchi=0;
                $tongxuatkho=0;
                $loinhuan=0;
                $tienmat=0;

                //Tổng thu
                $results = $this->showdulieu_thu_thang($idbep, $thang);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }

                //Tổng chi
                $results1 = $this->showdulieu_chi_thang($idbep, $thang);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }

                //Tổng xuất kho
                $results2 = $this->showdulieu_xuatkho_thang($idbep, $thang);
                if ($results2->getKK_TONG_KHO() != null) {
                $tongxuatkho=$results2->getKK_TONG_KHO();
                }

                $loinhuan=$tongthu - $tongchi - $tongxuatkho;
                $tienmat=$tongthu - $tongchi;
                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                $xtemplate->assign('TUAN', $tuan);
                $xtemplate->assign('TUNGAY', $results->getMIN_XUAT_NGAY_XUAT());
                $xtemplate->assign('DENNGAY', $results->getMAX_XUAT_NGAY_XUAT());
                $xtemplate->assign('LOINHUAN', Utility::formatToCurrencyString($loinhuan));
                $xtemplate->assign('TIENMAT', Utility::formatToCurrencyString($tienmat));
                $xtemplate->parse('main.thongkethang');

            }else{
                $tongthu=0;
                $tongchi=0;
                $loinhuan=0;
                $tienmat=0;
                $thang_=  $thang-1; 
                $tongtienxuatkho=0;
                $tongtienxuatkhotuannay=0;
                $tongtienxuatkhotuantruoc=0;

                  //Tổng thu
                $results = $this->showdulieu_thu_thang($idbep, $thang);
                if ($results->getXUAT_TONG_TIEN_PHAI_THU() != null) {
                $tongthu=$results->getXUAT_TONG_TIEN_PHAI_THU();
                }

                //Tổng chi
                $results1 = $this->showdulieu_chi_thang($idbep, $thang);
                if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $tongchi=$results1->getNHAP_TONG_TIEN_PHAI_TRA();
                }

                //Tổng xuất kho
                $results_ = $this->showdulieu_xuatkho_thang($idbep, $thang_);
                $tongtienxuatkhotuantruoc=$results_->getKK_TONG_KHO();

                $results2 = $this->showdulieu_xuatkho_thang($idbep, $thang);
                $tongtienxuatkhotuannay=$results2->getKK_TONG_KHO();

                $tongtienxuatkho=$tongtienxuatkhotuantruoc-$tongtienxuatkhotuannay;

               
                $loinhuan=$tongthu - $tongchi - $tongtienxuatkho;
                $tienmat=$tongthu - $tongchi;

                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                $xtemplate->assign('TUAN', $tuan);
                $xtemplate->assign('TUNGAY', $results->getMIN_XUAT_NGAY_XUAT());
                $xtemplate->assign('DENNGAY', $results->getMAX_XUAT_NGAY_XUAT());
                $xtemplate->assign('LOINHUAN', Utility::formatToCurrencyString($loinhuan));
                 $xtemplate->assign('TIENMAT', Utility::formatToCurrencyString($tienmat));
                $xtemplate->parse('main.thongkethang');  
            }

            //Kết thúc thống kê theo tháng
           


            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

   

    private function getMaxThang() {
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->getMaxThang($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getMaxTuan($idthang) {
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $kiemke->setTHANG_ID($idthang);
            
            $result = $kiemke->getMaxTuan($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function showdulieu_xuatkho($idbep, $thang, $tuan) {
        $db = new Database();
        try {
           $kiemke= new Kiem_ke();

            $result = $kiemke->showdulieu($idbep, $thang, $tuan, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

     private function showdulieu_chi($idbep, $thang, $tuan) {
        $db = new Database();
        try {
            $nhap = new Nhap();
            $result = $nhap->showdulieu($idbep, $thang, $tuan, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

     private function showdulieu_chi_thang($idbep, $thang) {
     
        $db = new Database();
        try {
            $nhap = new Nhap();
            $result = $nhap->showdulieu_thang($idbep, $thang, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

     private function showdulieu_thu($idbep, $thang, $tuan) {
        $db = new Database();
        try {
           $xuat= new Xuat();

            $result = $xuat->showdulieu($idbep, $thang, $tuan, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }


        private function showdulieu_thu_thang($idbep, $thang) {
     
        $db = new Database();
        try {
            $xuat= new Xuat();

            $result = $xuat->showdulieu_thang($idbep, $thang, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }


    private function showdulieu_xuatkho_thang($idbep, $thang) {
     
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->showdulieu_thang($idbep, $thang, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

     private function showdulieu_xuatkho_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY) {
     
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->showdulieu_xuatkho_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getBepId($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
